// https://objcer.com/2018/02/27/Explore-Gulp/
//@ts-check
var path=require('path')
var gulp = require('gulp');
var browserify = require('browserify');  
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var header = require('gulp-header');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');

const shell = require('gulp-shell')
// var connect = require("gulp-connect");

//jsdoc生成markdown
const jsdoc2md = require('jsdoc-to-markdown');
const fs = require('fs');

// using data from package.json
var pkg = require(path.resolve(__dirname,'./package.json'));
var banner = ['/**',
  ' * <%= pkg.name %> - <%= pkg.description %>',
  ' * @version v<%= pkg.version %>',
  ' * @link <%= pkg.homepage %>',
  ' * @license <%= pkg.license %>',
  ' */',
  ''].join('\n');

// 编译ES6
gulp.task('es6', function() {
    browserify({
        insertGlobals: true,
        entries: './src/index.js',
        debug: false
      })
    .transform(babelify.configure({
        // presets : ["es2015"]
        babelrc: false,
        presets: ['@babel/preset-env']
    }))
    .bundle()
    .pipe(source('rx.tools.js'))
    .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify()) // now gulp-uglify works 
    .on('error', gutil.log)
    .pipe(header(banner, { pkg : pkg } ))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./dist/'))
    .pipe(gulp.dest('./vuepress/'));

});

gulp.task('markdown',function(){


    // jsdoc2md.render({ files: './*/*.js' }).then((res)=>{
    //     console.log(res)
    // })
    const array = '#Array\n' + jsdoc2md.renderSync({ files: 'src/ext/array.js' });
    const date = '#Date\n' + jsdoc2md.renderSync({ files: 'src/ext/date.js' });
    const object = '#Object\n' + jsdoc2md.renderSync({ files: 'src/ext/object.js' });
    const number = '#Number\n' + jsdoc2md.renderSync({ files: 'src/ext/number.js' });
    const common = '#Common\n' + jsdoc2md.renderSync({ files: 'src/common/index.js' });

    const element = '#Element\n' + jsdoc2md.renderSync({ files: 'src/brower/element.js' });
    const env = '#Env\n' + jsdoc2md.renderSync({ files: 'src/brower/env.js' });
    const event = '#Event\n' + jsdoc2md.renderSync({ files: 'src/brower/event.js' });


    fs.writeFileSync('docs/ext/array.md', array)
    fs.writeFileSync('docs/ext/date.md', date)
    fs.writeFileSync('docs/ext/number.md', number)
    fs.writeFileSync('docs/ext/object.md', object)
    fs.writeFileSync('docs/common.md', common)

    fs.writeFileSync('docs/brower/element.md',element)
    fs.writeFileSync('docs/brower/env.md', env)
    fs.writeFileSync('docs/brower/event.md', event)
})

gulp.task('vuepress',shell.task('vuepress build docs'))

// 监视文件，自动执行
// gulp.task('watch', function () {
//     gulp.watch('./rx.tools/*.js', ['es6']);
// });

// 创建一个WEB服务
// gulp.task('connect', function () {
//     connect.server({
//         root: './',
//         livereload: true,
//         port: 4000
//     });
// });
//@ts-ignore
gulp.task('default', ['markdown','vuepress','es6']);